package com.greateamd.timetableasy.client.mvc.view.crud.event;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.mvc.model.Model;
import com.greateamd.timetableasy.client.services.event.EventService;
import com.greateamd.timetableasy.client.services.event.EventServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;
import com.greateamd.timetableasy.client.widgets.ClassicLargeLabel;
import com.greateamd.timetableasy.client.widgets.DialogHandler;
import com.greateamd.timetableasy.client.widgets.DialogPopupPanel;
import com.greateamd.timetableasy.client.widgets.MessagePopupPanel;
import com.greateamd.timetableasy.client.widgets.event.EventPanel;
import com.greateamd.timetableasy.domain.Event;
import com.greateamd.timetableasy.domain.User;

public class EditEventPage <M extends Model> extends IEditEventPage<M>{

	private final EventServiceAsync eventService = GWT.create(EventService.class);

	@UiTemplate("EditEventPage.ui.xml")
	interface EditEventPageUIBinder extends UiBinder<Widget, EditEventPage> {}
	private static EditEventPageUIBinder uiBinder = GWT.create(EditEventPageUIBinder.class);

	//Event panel
	@UiField EventPanel eventPanel;
	
	@UiField Button submitBt;
	@UiField Button deleteBt;
	
	private Event modelEvent;
	
	/**
	 * Constructor
	 */
	public EditEventPage(final Event event) {
	    initWidget(uiBinder.createAndBindUi(this));
	    modelEvent = event;
	    fillEventPanel();
	}
	
	@Override
	public Widget getWidgetPage() {
		return this;
	}

	@Override
	public void setUser(User user) {
	}


	//---------------------------------------------------------------------
	//-------------------------- UI action
	public void fillEventPanel() {
		eventPanel.setEvent(modelEvent);
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	
	@UiHandler("submitBt")
	void onSubmitBtClick(ClickEvent e){
		editEvent();
	}
	@UiHandler("deleteBt")
	void onDeleteBtClick(ClickEvent e){
		new DialogPopupPanel(new ClassicLargeLabel("Do you really want delete this event ?"), new DialogHandler() {
			@Override
			public void onConfirmClick() {
				deleteEvent();
			}
			@Override
			public void onCancelClick() {
			}
		});
	}

	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	private void editEvent() {
		if(!checkFormValidity()){
			Window.alert("Invalid form, you must valid and apply your form");
			return;
		}
		new ServerCall<Boolean>() {

			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				modelEvent = eventPanel.getEvent();
				eventService.update(modelEvent,asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				if(success){
					new MessagePopupPanel("OK","This event has well been edited").show();
				} else {
					new MessagePopupPanel("Error","Sorry, your operation failed").show();
				}
			}
		}.call();
	}
	
	private void deleteEvent() {
		if(eventPanel.getEvent().getId()==null ){
			Window.alert("Invalid form, event isn't apt for this operation");
			return;
		}
		new ServerCall<Boolean>() {

			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				modelEvent = eventPanel.getEvent();
				eventService.remove(modelEvent,asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				if(success){
					new MessagePopupPanel("OK","This event has well been removed").show();
				} else {
					new MessagePopupPanel("Error","Sorry, your operation failed").show();
				}
			}
		}.call();
	}
	
	private boolean checkFormValidity() {
		if(eventPanel.getEvent()==null) return false;
		if(!eventPanel.eventPanelIsValid()) return false;
		return true;
	}
}
